import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_markdown/flutter_markdown.dart';
import 'package:get/get.dart';
import 'package:sj001/utils/Utils.dart';

import '../../model/ChatMsgItem.dart';

class ChatItem extends StatelessWidget {
  ChatItem(this.item, {this.onLike, Key? key}) : super(key: key);
  Rx<ChatMsgItem> item;
  Function(Rx<ChatMsgItem>)? onLike;

  @override
  Widget build(BuildContext context) {
    return Obx(
      () {
        String content = '';
        item.value.models.forEach((element) {
          content += element.answer ?? '';
        });
        if (!item.value.isDone.value) {
          content += '...';
        }
        return Padding(
          padding: EdgeInsets.only(
            left: item.value.user == 1 ? 50 : 15,
            right: item.value.user == 1 ? 15 : 50,
            bottom: 5,
            top: 5,
          ),
          child: Row(
            mainAxisAlignment: item.value.user == 1
                ? MainAxisAlignment.end
                : MainAxisAlignment.start,
            children: [
              Flexible(
                child: Container(
                  padding: const EdgeInsets.all(10),
                  decoration: BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.circular(8),
                    border: Border.all(color: Colors.black, width: 0.5),
                  ),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.end,
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      MarkdownBody(
                        shrinkWrap: true,
                        data: content,
                        selectable: true,
                      ),
                      Row(
                        mainAxisSize: MainAxisSize.min,
                        children: [
                          // Visibility(
                          //   visible: item.value.createAt.value > 0,
                          //   child: Text(
                          //     Utils.strFromMilseconds(
                          //         item.value.createAt.value),
                          //   ),
                          // ),
                          Visibility(
                            visible: item.value.user == 0,
                            child: InkWell(
                              onTap: () => onLike?.call(item),
                              child: Padding(
                                padding:
                                    const EdgeInsets.symmetric(vertical: 10),
                                child: Icon(
                                    item.value.isLike.value
                                        ? Icons.favorite
                                        : Icons.favorite_border,
                                    size: 25),
                              ),
                            ),
                          ),
                        ],
                      ),
                    ],
                  ),
                ),
              ),
            ],
          ),
        );
      },
    );
  }
}
